package com.bbseguros.pesquisador.util;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import com.bbseguros.pesquisador.gps.BVSendGps;

public class DateUtils {

	private static Calendar cal = null;	
	public static final String DDMMYYYY_FORMAT = "dd/MM/yyyy";
	public static final String DDMMYYYYHHMMSS_FORMAT = "dd/MM/yyyy hh:MM:ss";
	public static final String DDMMYYYYHHMM_FORMAT = "dd/MM/yyyy hh:MM";
	public static final String YYYYMMDD_FORMAT = "yyyyMMdd";
	public static final String hhmmss_FORMAT = "hh:mm:ss";
	public static final String MMDDhhmmss_FORMAT = "MMDDhhmmss";
	public static final String yyyyMMdd_HHMMSS_FORMAT ="yyyy-MM-dd HH:MM:SS";
	
	private static Locale l;
	private static TimeZone z;
	
	public DateUtils(){
		
		cal = Calendar.getInstance();
	}
	
	public static Date getActualDateTime() {
		
		l = new Locale("pt_BR");
		z = TimeZone.getTimeZone("GMT-03:00");			
		Calendar cal = Calendar.getInstance(z,l);
		//Calendar cal = Calendar.getInstance();
		
		Date dt = cal.getTime();
		return dt;
	}

	public static String getDate(String format) {

		String date = null;
		//cal = Calendar.getInstance(z,l);
		cal = Calendar.getInstance();
		Date now = cal.getTime();
		
		SimpleDateFormat SQL_DATE_FORMAT = new SimpleDateFormat(format);

		date = SQL_DATE_FORMAT.format(now);
		if (date.length() == 20) {
			date = date.substring(0,19);
		}

		return date;
	}
	

	public String getActualStringDateTime() {
		cal = Calendar.getInstance();

		StringBuffer dt = new StringBuffer();
		dt.append(getYear() + "-");
		dt.append(getMonth() + "-");
		dt.append(getDay() + " ");
		dt.append(getHour() + ":");
		dt.append(getMinute() + ":");
		dt.append(getSeconds());
		return dt.toString();
	}

	public String getDay() {
		String day = String.valueOf(cal.get(Calendar.DATE));
		return fillVar(day);
	}

	public String getMonth() {
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		return fillVar(month);
	}

	public String getYear() {
		String year = String.valueOf(cal.get(Calendar.YEAR));
		return fillVar(year);
	}
	
	public String getHour() {
		String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
		return fillVar(hour);
	}
	
	public String getMinute() {
		String minute = String.valueOf(cal.get(Calendar.MINUTE));
		return fillVar(minute);
	}
	
	public String getSeconds() {
		String second = String.valueOf(cal.get(Calendar.SECOND));
		return fillVar(second);
	}
	
	public String getMilliSeconds() {
		String milliSecond = String.valueOf(cal.get(Calendar.MILLISECOND));
		if (milliSecond.length() < 2) {
			milliSecond = "00" + milliSecond;
		} else if (milliSecond.length() < 3) {
			milliSecond = "0" + milliSecond;
		}
		return milliSecond;
	}
	
	private String fillVar (String var){
		if (var.length() < 2)
			var = "0" + var;
		return var;
	}
	
	public static String getDateInString(Date date, String format){
		DateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
				
		
	}
	
	public static String formatSimpleDate(String value){
		return (value.substring(2)+"/"+value.substring(0, 2));
	}
	
	public static String formatMySqlDate(String value){
		return value.substring(6) + "-" +
				value.substring(3,5) + "-" +
				value.substring(0,2);
	}
	
	public static String unformatMySqlDate(String value){
		return value.substring(8) + "/" +
				value.substring(5,7) + "/" +
				value.substring(0,4);
	}
	
	/**
	 * @param value - HHmmSS = HH:MM
	 * @return
	 */
	public static String formatSimpleHour(String value){
		return (value.substring(0, 2)+":"+value.substring(2,4));
	}
	
	public static void main(String[] args) {
		System.out.println(DateUtils.unformatMySqlDate("2009-12-27"));
	}
}
